package cn.itsource.xhy.order.service.impl;

import cn.itsource.xhy.basic.service.impl.BaseServiceImpl;
import cn.itsource.xhy.order.domain.OrderAddress;
import cn.itsource.xhy.order.domain.OrderAdopt;
import cn.itsource.xhy.order.mapper.OrderAddressMapper;
import cn.itsource.xhy.order.mapper.OrderAdoptMapper;
import cn.itsource.xhy.order.service.IOrderAdoptService;
import cn.itsource.xhy.pay.contants.BusinessContants;
import cn.itsource.xhy.pay.domain.PayBill;
import cn.itsource.xhy.pay.service.IPayBillService;
import cn.itsource.xhy.pet.domain.Pet;
import cn.itsource.xhy.pet.mapper.PetMapper;
import cn.itsource.xhy.quartz.domain.QuartzJobInfo;
import cn.itsource.xhy.quartz.service.IQuartzService;
import cn.itsource.xhy.user.domain.User;
import cn.itsource.xhy.user.domain.UserAddress;
import cn.itsource.xhy.user.mapper.UserAddressMapper;
import cn.itsource.xhy.utils.CodeGenerateUtils;
import cn.itsource.xhy.utils.LoginContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt> implements IOrderAdoptService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IQuartzService quartzService;

    @Override
    @Transactional
    public String createOrder(Map<String, String> map, HttpServletRequest request) {
        Long petId = Long.valueOf(map.get("pet_id"));
        Long addressId = Long.valueOf(map.get("address_id"));
        String paymentWay = map.get("payment_way");
        User user = (User) LoginContext.getObject(request);
        // 下架宠物,拿到宠物对象
        Pet pet = petMapper.findByPetId(petId);
        // 设置下架、下架时间
        pet.setState(0);
        pet.setOffsaletime(new Date());
        // 修改pet对象
        petMapper.update(pet);

        // 创建订单
        OrderAdopt orderAdopt = createOrderAdopt(pet,user);
        orderAdoptMapper.save(orderAdopt);

        // 订单地址，传入的地址id查询
        UserAddress userAddress = userAddressMapper.findById(addressId);
        // 保存到orderAddress
        OrderAddress orderAddress = userAddressToOrderAddress(userAddress);
        // 补全信息
        orderAddress.setOrder_id(orderAdopt.getId());
        orderAddress.setOrderSn(orderAdopt.getOrderSn());
        // 保存
        orderAddressMapper.save(orderAddress);

        PayBill payBill = createPayBil(orderAdopt);
        // 补全信息
        payBill.setPayChannel(paymentWay);
        payBill.setBusinessType(BusinessContants.ORDER_ADOPT);
        payBill.setBusinessKey(orderAdopt.getId());
        // 生成支付单子
        payBillService.save(payBill);
        //拉起收银台
        String result = payBillService.pay(payBill);

        //添加定时任务
        QuartzJobInfo jobInfo = new QuartzJobInfo();
        //用订单号设置名字
        jobInfo.setJobName(payBill.getOrderSn());
        Map<String, Object> param = new HashMap<>();
        param.put("orderSn",payBill.getOrderSn());
        //携带参数
        jobInfo.setParams(param);
        //设置触发时间
        jobInfo.setFireDate(payBill.getLastPayTime());
        //调用方法将参数传过去
        quartzService.addJob(jobInfo);

        return result;
    }

    @Override
    public OrderAdopt findByOrderSn(String orderSn) {
        return orderAdoptMapper.findByOrderSn(orderSn);
    }

    private PayBill createPayBil(OrderAdopt orderAdopt) {
        PayBill payBill = new PayBill();
        payBill.setDigest(orderAdopt.getDigest());
        payBill.setMoney(orderAdopt.getPrice());
//        /*默认0:待支付  1支付成功  -1:取消订单 */
        payBill.setState(0);
        payBill.setLastPayTime(orderAdopt.getLastPayTime());
        payBill.setCreateTime(new Date());
        payBill.setUser_id(orderAdopt.getUser_id());
        payBill.setShop_id(orderAdopt.getShop_id());
        payBill.setOrderSn(orderAdopt.getOrderSn());

        return payBill;
    }

    // 组装订单地址
    private OrderAddress userAddressToOrderAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setCreateTime(new Date());
        return orderAddress;
    }

    // 组装订单
    private OrderAdopt createOrderAdopt(Pet pet, User user) {
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("[领养订单]");
        orderAdopt.setState(0);
        orderAdopt.setPrice(pet.getSaleprice());
        orderAdopt.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        orderAdopt.setLastPayTime(new Date(System.currentTimeMillis()+60*1000*30));
        orderAdopt.setPet_id(pet.getId());
        orderAdopt.setUser_id(user.getId());
        orderAdopt.setShop_id(pet.getShop_id());
        return orderAdopt;
    }

}
